<?php
/**
 * @description 主播招募申请表
 */

namespace Plu\Library\Models\Platform\Recruit;

use Phalcon\Mvc\Model,
    Phalcon\Db\Column;

class ApplyRoomHost extends Model {
    public $Id;

    public $UserId;

    public $QQ;

    public $FavoriteGameId;

    public $Phone;

    public $Domain;

    public $RoomId;

    public $ChannelId;

    public $Status;

    public $ApplyTime;

    public $ApproveTime;

    public $ApproveNote;

    public $BanUserId;

    public $ClientIp;

    public $TrueName;

    public $IDCard;

    /**
     * 身份证正面照片
     */
    public $IDCardPicFront;

    /**
     * 身份证反面照片
     */
    public $IDCardPicBack;

    /**
     * 持身份证照片
     */
    public $IDCardPic;

    public $RoomPlayVideoType;

    public $Description;

    public $WorkLink;

    public $LivePermission;

    public $VideoPermission;

    const StatusType = array(
        'INIT'		=> 0, //未审批
        'ACCEPTED'  => 1, //通过
        'REJECTED'	=> 2,  //拒绝
        'BANNED'    => 3  //封停
    );

    const RoomType = array(
        'LIVE'      => 1,
        'VIDEO'		=> 2
    );

    const LiveType = array(
        'Tga'      => 1, //随心播
        'QQPush'   => 2, //直播访问qqpush
        'QQStream' => 4, //直接访问腾讯外站直播
        'OBS'      => 8,//obs推流
        'APP'      => 16,//app直播
    );

    const SignStatusType = [
        'PENDING' => 0,
        'ACCEPTED' => 1,
        'REJECTED' => 2,
        'UNWIND' => 3,
    ];

    public function initialize() {
        $this->setConnectionService('db.LongzhuAuth');
        $this->allowEmptyStringValues(['IDCardPicFront','IDCardPicBack','IDCardPic','QQ']);
    }

    public function getSource() {
        return "ApplyRoomHost";
    }

    public static function getSignRecruit($uid){
        return ApplyRoomHost    ::findFirst([
            'conditions' => 'UserId = :uid: AND Status = 1 AND LivePermission > 0 AND IsSign = 1',
            "bind" => array(
                'uid'   => $uid,
            ),
            "bindTypes" => array(
                'uid'   => Column::BIND_PARAM_INT,
            ),
            "order"     => "Id DESC"
        ]);
    }

    /**
     *
     */
    public static function getNoRejectedRecruits($uid, $type) {
        $sql = "UserId = :uid: AND Status != 2";
        if ($type == self::RoomType['LIVE']) {
            $sql .= " AND LivePermission > 0";
        } else {
            $sql .= " AND VideoPermission > 0";
        }

        $item = self::find(array(
            $sql,
            "bind" => array(
                'uid'	=> $uid,
            ),
            "bindTypes" => array(
                'uid'	=> Column::BIND_PARAM_INT,
            ),
            "order"		=> "Id DESC"
        ));

        return $item;
    }

    /**
     * 获取最后一次申请的状态
     */
    public static function getNoRejectedRecruit($uid, $type) {
        $sql = "UserId = :uid: AND Status != 2";
        if ($type == self::RoomType['LIVE']) {
            $sql .= " AND LivePermission > 0";
        } else {
            $sql .= " AND VideoPermission > 0";
        }

        $item = self::findFirst(array(
            $sql,
            "bind" => array(
                'uid'	=> $uid,
            ),
            "bindTypes" => array(
                'uid'	=> Column::BIND_PARAM_INT,
            ),
            "order"		=> "Id DESC"
        ));

        return $item;
    }

    /**
     * @method 通过用户id获取申请
     * @param int $userid 用户id
     * @param int $playType 权限类型1直播2点播
     * @return mixed
     */
    public static function getRecruitByUid($uid, $type){
        $sql = "UserId = :uid:";
        if ($type == self::RoomType['LIVE']) {
            $sql .= " AND LivePermission > 0";
        } else {
            $sql .= " AND VideoPermission > 0";
        }

        $item = self::findFirst(array(
            $sql,
            "bind" => array(
                'uid'	=> $uid,
            ),
            "bindTypes" => array(
                'uid'	=> Column::BIND_PARAM_INT,
            ),
            "order"		=> "Id DESC"
        ));

        return $item;
    }

    /**
     * @method 通过应用id获取申请
     * @param int $id
     * @param int $playType 权限类型1直播2点播
     * @return mixed
     */
    public static function getRecruitById($id, $type, $status=self::StatusType['ACCEPTED']){
        $sql = "Id = :id: AND Status = :status:";
        if ($type == self::RoomType['LIVE']) {
            $sql .= " AND LivePermission > 0";
        } else {
            $sql .= " AND VideoPermission > 0";
        }

        $item = self::findFirst(array(
            $sql,
            "bind"	=> array(
                'id'	=> $id,
                'status'=> $status	
            ),
            "bindTypes" => array(
                'id'	=> Column::BIND_PARAM_INT,
                'status'=> Column::BIND_PARAM_INT
            ),
            "order"		=> "Id DESC"
        ));

        return $item;
    }

    /**
     * @method 身份证被注册次数
     * @param string $idcard 身份证号
     * @param int $playType 权限类型1直播2点播
     * @param int $status 申请状态，默认为成功1
     * @return int 身份证被注册次数
     */
    public static function getCountOfRegistedIdcard($idcard, $type, $status=self::StatusType['ACCEPTED']) {
        $sql = 'IDCard = :idcard: AND Status = :status: ';
        if ($type == self::RoomType['LIVE']) {
            $sql .= " AND LivePermission > 0";
        } else {
            $sql .= " AND VideoPermission > 0";
        }

        return self::Count(array(
            $sql,
            'bind' => array(
                'idcard'	=> $idcard,
                'status'	=> $status
            ),
            'bindTypes'	=> array(
                'idcard'	=> Column::BIND_PARAM_STR,
                'status'	=> Column::BIND_PARAM_INT
            )
        ));
    }

    /**
     * @method 通过用户Id和游戏id获取申请
     * @param int $userid
     * @param int $gameid
     * @param int $playType 权限类型1直播2点播
     * @return mixed
     */
    public static function getRecruitByUseridAndGameid($userid, $type, $gameid, $status=self::StatusType['ACCEPTED']) {
        $sql = "UserId = :uid: AND FavoriteGameId = :gameid: AND Status = :status:";
        if ($type == self::RoomType['LIVE']) {
            $sql .= " AND LivePermission > 0";
        } else {
            $sql .= " AND VideoPermission > 0";
        }
        $item = self::findFirst(array(
            $sql,
            "bind"	=> array(
                "uid"	=> $userid,
                "gameid"=> $gameid,
                "status"=> $status
            ),
            "bindTypes" => array(
                "uid"	=> Column::BIND_PARAM_INT, 
                "gameid"=> Column::BIND_PARAM_INT, 
                "status"=> Column::BIND_PARAM_INT
            ),
            "order"		=> "Id DESC"
        ));

        return $item;
    }

    public static function getRecruitByUseridAndStatus($userid, $type, $status=self::StatusType['ACCEPTED']) {
        $sql = "UserId = :uid: AND Status = :status:";
        if ($type == self::RoomType['LIVE']) {
            $sql .= " AND LivePermission > 0";
        } else {
            $sql .= " AND VideoPermission > 0";
        }
        $item = self::findFirst(array(
            $sql,
            "bind"  => array(
                "uid"   => $userid,
                "status"=> $status
            ),
            "bindTypes" => array(
                "uid"   => Column::BIND_PARAM_INT, 
                "status"=> Column::BIND_PARAM_INT
            ),
            "order"     => "Id DESC"
        ));

        return $item;
    }
    
}

?>

